package test.baojia.act.operation;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
import org.testng.Assert;
import test.baojia.act.AbstractAct;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public abstract class Operation extends AbstractAct {

    public enum OPERATION {
        byType,
        byExp,
        byValue,
        byValueType,
        byText,
        byVisable,
        bySelect,
        byCheck,
        byOptions ,
        byAttribute ,
        byFileName ,
        byGlobalProperty}

    public enum ByType {ID, NAME, XPATH, SELECTOR}

    protected By makeBy(){
        final String byType = (String) this.context.get(OPERATION.byType.toString());
        final String byExp = (String) this.context.get(OPERATION.byExp.toString());

        By by = null;
        if(ByType.ID.toString().equals(byType)){
            by = By.id(byExp);
        }else if(ByType.NAME.toString().equals(byType)){
            by = By.name(byExp);
        }else if(ByType.XPATH.toString().equals(byType)){
            by = By.xpath(byExp);
        }else if(ByType.SELECTOR.toString().equals(byType)){
            by = By.cssSelector(byExp);
        }
        Assert.assertNotNull(by);

        return by;
    }

    protected WebElement makeWebElement(boolean check){
        WebElement we = this.driver.findElement(this.makeBy());
        if(check){
            Assert.assertNotNull(we);
            Assert.assertTrue(we.isEnabled());
            Assert.assertTrue(we.isDisplayed());
        }
        return we;
    }

    protected List<WebElement> makeWebElements(){
        return this.driver.findElements(this.makeBy());
    }

    protected Select makeSelectWebElement(boolean check){
        WebElement we = this.driver.findElement(this.makeBy());
        if(check){
            Assert.assertNotNull(we);
            Assert.assertTrue(we.isEnabled());
        }
        return new Select(we);
    }

    public final static Map makeSimpleContext(String byType , String byExp , String byValue){
        Map<String , Object> context = new HashMap();
        context.put(Operation.OPERATION.byType.toString() , byType);
        context.put(Operation.OPERATION.byExp.toString(), byExp);
        context.put(Operation.OPERATION.byValue.toString() , byValue);
        return context;
    }

    public final static Map emptyContext(){
        Map<String , Object> context = new HashMap();
        return context;
    }
}
